package socdev.mobile;

import socdev.data.Message;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.app.Activity;

public class MessageAdapter extends ArrayAdapter<Message>{

	Context context;
	int layoutResourceId;
	Message[] data;
	public MessageAdapter(Context context, int textViewResourceId, Message[] objects) {
		super(context, textViewResourceId, objects);
		this.context = context;
		this.layoutResourceId = textViewResourceId;
		this.data = objects;
	}
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        MessageHolder holder = null;
        
        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            
            holder = new MessageHolder();
            holder.datetime = (TextView)row.findViewById(R.id.datetime);
            holder.from = (TextView)row.findViewById(R.id.from);
            holder.message = (TextView)row.findViewById(R.id.message);
            
            row.setTag(holder);
        }
        else
        {
            holder = (MessageHolder)row.getTag();
        }
        
        Message message = data[position];
        holder.datetime.setText(message.datetime);
        holder.from.setText(message.from);
        holder.message.setText(message.text);
        return row;
    }
	
	static class MessageHolder
    {
        TextView datetime;
        TextView from;
        TextView message;
    }

}
